<template>
  <div>
    <el-container>
      <el-header height="70px">
        <div style="height:20px; font-size: 15px;background-color: rgba(205,205,205,0.2)">
          <div style="float: left; margin-left: 20px">
            <div>系统模块 > 码表管理</div>
          </div>
        </div>
        <div style="height: 40px;margin-top: 10px;min-height: 60px;font-size: 20px">
          <el-form ref="selectDict" :model="selectDict" :inline="true" label-width="70px">
            <el-col :span="7">
              <el-form-item label="码表名称" prop="dictLabel">
                <el-input size="mini" v-model="selectDict.dictLabel" style="width: 90%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="码表类型" prop="dictType">
                <el-input size="mini" v-model="selectDict.dictType" style="width: 90%;"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="7">
              <el-form-item label="是否可用" prop="status">
                <el-select size="mini" v-model="selectDict.status">
                  <el-option v-for="item in dictYON" :key="item.dictCode" :label="item.dictLabel"
                             :value="item.dictValue"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="3">

              <el-button type="primary" @click="getDictPage()" size="mini">确定</el-button>
              <el-button @click="toRestFild('selectDict')" size="mini">重置</el-button>

            </el-col>
            <!--            <el-col :span="2" :offset="1">-->
            <!--            </el-col>-->
          </el-form>
        </div>
      </el-header>
      <el-main>
        <div style="float: left">
          <el-button @click="addOper()" type="danger" size="mini">添加</el-button>
        </div>
        <el-table
          :data="tableData"
          style="width: 100%;margin-bottom: 20px;"
          row-key="dictCode"
          height="350"
          default-expand-all>
          <el-table-column
            prop="dictLabel"
            label="码表名称">
          </el-table-column>
          <el-table-column
            prop="dictType"
            label="码表类型"
            sortable>
          </el-table-column>
          <el-table-column
            prop="dictValue"
            label="码表键值">
          </el-table-column>
          <el-table-column
            label="是否可用">
            <template slot-scope="scope">
              <span>{{ getYesOrNo(scope.row.status, dictYON) }}</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="createTime"
            label="创建时间">
          </el-table-column>
          <el-table-column
            prop="updateTime"
            label="更新时间">
          </el-table-column>
          <el-table-column label="操作"
                           align="center"
          >
            <template slot-scope="scope">
              <el-button @click.native.prevent="editDict(scope)"
                         type="text"
              >编辑
              </el-button>
              <el-button @click.native.prevent="deleteDict(scope)"
                         type="text"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage1"
          :page-size="pageSize"
          layout="total, prev, pager, next"
          :total="total">
        </el-pagination>
        <el-dialog
          :before-close="handleClose"
          :title="title"
          :visible.sync="dialogVisible"
          width="60%">
          <div style="height: 200px">
            <el-form ref="dict" :model="dict" :inline="true" label-width="150px">
              <el-col :span="12">
                <el-form-item label="id:" prop="dictCode">
                  <el-input v-model="dict.dictCode" disabled></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :rules="[{ required: true, message: '请输入码表类型（大写英文）', trigger: 'blur' }]" label="码表类型:"
                              prop="dictType">
                  <el-input v-model="dict.dictType" @change="justUpChart"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :rules="[{ required: true, message: '请输入码表名称', trigger: 'blur' }]" label="码表名称:"
                              prop="dictLabel">
                  <el-input v-model="dict.dictLabel"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :rules="[{ required: true, message: '请输入码表键值', trigger: 'blur' }]" label="码表键值:"
                              prop="dictValue">
                  <el-input v-model="dict.dictValue"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :rules="[{ required: true, message: '请选择是否可用', trigger: 'blur' }]" label="是否可用:"
                              prop="status">
                  <el-select v-model="dict.status">
                    <el-option v-for="item in dictYON" :key="item.dictCode" :label="item.dictLabel"
                               :value="item.dictValue"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item :rules="[{ required: true, message: '请输入排序', trigger: 'blur' },
                 { type: 'number', message: '排序必须为数字值'}]" label="排序:"
                              prop="dictSort">
                  <el-input v-model.number="dict.dictSort" type="number"></el-input>
                </el-form-item>
              </el-col>
            </el-form>
          </div>
          <span style="float: bottom; margin-bottom: 30px" slot="footer" class="dialog-footer">
                    <el-button @click="cancel()">取 消</el-button>
                  <el-button type="primary" @click="sureDict()">确 定</el-button>
                </span>
        </el-dialog>
      </el-main>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "dictModule",
  data() {
    return {
      dictYON: [],
      tableData: [],
      selectDict: {
        dictLabel: '',
        dictType: '',
        status: '',
      },
      title: '',
      dialogVisible: false,
      dict: {
        dictCode: '',
        dictSort: '',
        dictLabel: '',
        dictValue: '',
        dictType: '',
        status: '',
        remark: '',
      },
      currentPage1: 1,
      pageSize: 10,
      total: 0,
    }
  },
  mounted() {
    this.$getDict("YESORNO").then(res => {
      this.dictYON = res
      console.log(this.dictYON)
    })
    this.getDictPage();
  },
  methods: {
    justUpChart(){
      this.dict.dictType = this.dict.dictType.replace(/[^A-Z]/g, '');
    },
    getDictPage() {
      this.$axios({
        url: '/dict/getDictPage',
        method: "get",
        params: {
          ...this.selectDict,
          pageNo: this.currentPage1,
          pageSize: this.pageSize
        }
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data.records
          this.total = res.data.data.total
        } else {
          this.$message.error("系统出错，请联系管理员")
        }
      })
    },
    toRestFild(ref) {
      this.$nextTick(() => {
        this.$refs[ref].resetFields()
      })
    },
    handleClose() {
      this.toRestFild('dict')
      this.dialogVisible = false
    },
    getYesOrNo(value, list) {
      let res = this.$getDictLabel(value, list)
      return res
    },
    handleCurrentChange(val) {
      this.currentPage1 = val
      this.getDictPage()
    },
    editDict(scope) {
      let row = scope.row
      this.title = '修改码表'
      this.$nextTick(() => {
        this.dict = {
          dictCode: row.dictCode,
          dictSort: row.dictSort,
          dictLabel: row.dictLabel,
          dictValue: row.dictValue,
          dictType: row.dictType,
          status: row.status,
          remark: row.remark,
        }
      })
      this.dialogVisible = true
    },
    addOper() {
      this.title = '新增码表'
      this.toRestFild('dict')
      this.dialogVisible = true
    },
    sureDict() {
      let url = this.title === '新增码表' ? '/dict/addDict' : '/dict/editDict'
      this.$refs['dict'].validate(res => {
        this.$axios({
          url: url,
          method: 'post',
          data: {
            ...this.dict
          }
        }).then(res => {
          if (res.data.code === 200) {
            this.$message.success("操作成功！")
            this.getDictPage()
            this.toRestFild('oper')
            this.dialogVisible = false
          } else {
            this.$message.error("操作失败！")
          }
        })
      })

    },
    deleteDict(scope) {
      this.$confirm('确认删除？')
        .then(_ => {
          let id = scope.row.dictCode
          this.$axios({
            url: '/dict/deleteDict',
            method: 'delete',
            params: {
              id: id
            }
          }).then(res => {
            if (res.data.code === 200) {
              this.$message.success("删除成功！")
              this.getDictPage()
            } else {
              this.$message.error("删除失败！")
            }
          })
        })
        .catch(_ => {
        });
    },
    cancel() {
      this.toRestFild('dict')
      this.dialogVisible = false
    }
  }
}
</script>

<style scoped>

</style>
